package com.example.jiradata.service;

import com.example.jiradata.jpa.JiraIssueTypeRepository;
import com.example.jiradata.jpa.JiraUserRepository;
import com.example.jiradata.pojo.JiraIssueType;
import com.example.jiradata.pojo.JiraUser;
import com.example.jiradata.utils.HttpClientUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class JiraUserService {

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    private JiraUserRepository jiraUserRepository;

    public void syncUser() throws IOException {
        List<JiraUser> res = httpClientUtils.exchange("https://tngasia.atlassian.net/rest/api/2/users/search",new ParameterizedTypeReference<List<JiraUser>>(){},null, HttpMethod.GET);

        for (JiraUser jiraUser : res) {
            System.out.println(jiraUser.getAccountId());
            jiraUserRepository.save(jiraUser);
        }
    }

}
